package com.zbkj.service.config;

import cn.binarywang.wx.miniapp.api.WxMaOrderShippingService;
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.impl.WxMaOrderShippingServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaQrcodeServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.zbkj.dal.base.constants.WeChatConstants;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class WechatConfiguration {

    @Resource
    private WxConfigProperties properties;

    @Value("${wx.miniapp.secret}")
    private String secret;

    @Bean
    public WxPayService wxService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setCertSerialNo("4FAA87FB6246F4B3520CF2406E7F0CC319774DA7");
        payConfig.setAppId(properties.getAppId());
        payConfig.setMchId(properties.getMchId());
        // payConfig.setMchKey(properties.getMchKey());
        payConfig.setApiV3Key("8i2j1sft554w6jbzfvz6flo6m50y9ji7");
        payConfig.setKeyPath(properties.getCertKeyPath());
        payConfig.setPrivateCertPath(properties.getPrivateCertPath());
        payConfig.setPrivateKeyPath(properties.getPrivateKeyPath());
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    @Bean
    public WxMaQrcodeService wxMaQrcodeService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setMsgDataFormat(WeChatConstants.WECHAT_MINI_MSG_DATA_FORMAT);
        config.setAppid(properties.getAppId());
        config.setSecret(secret);
        config.setToken(Strings.EMPTY);
        config.setAesKey(Strings.EMPTY);
        WxMaServiceImpl wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        return new WxMaQrcodeServiceImpl(wxMaService);
    }

    @Bean
    public WxMaOrderShippingService wxMaOrderShippingService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setMsgDataFormat(WeChatConstants.WECHAT_MINI_MSG_DATA_FORMAT);
        config.setAppid(properties.getAppId());
        config.setSecret(secret);
        config.setToken(Strings.EMPTY);
        config.setAesKey(Strings.EMPTY);
        WxMaServiceImpl wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        return new WxMaOrderShippingServiceImpl(wxMaService);
    }
}
